package com.mischa.atc.tests;

import junit.framework.Assert;

import org.junit.Test;

import com.mischa.atc.Aircraft;
import com.mischa.atc.Flight;

public class FlightTest {
	private static final float WITHIN_PRECISION = 1E-6f;
	
	@Test
	public void testThatSpeedIsNMPerHourAndUpdateIsPerSecond()
	{
		Aircraft aircraft = new Aircraft();
		aircraft.setSpeed(250);
		aircraft.setHeading(90);
		
		Flight flight = new Flight(aircraft);
		
		// Step 3600 seconds, i.e. 1 hour
		flight.update(3600f);
		
		// Distance is 250 nm to the west
		Assert.assertEquals(250f, aircraft.getPosition().getX(), WITHIN_PRECISION);
		Assert.assertEquals(0f,   aircraft.getPosition().getY(), WITHIN_PRECISION);
	}
	
	@Test
	public void testThatFlightPositionIsUpdatedAfterSweep()
	{
		Aircraft aircraft = new Aircraft();
		aircraft.setSpeed(250);
		aircraft.setHeading(-30);
		
		Flight flight = new Flight(aircraft);
		
		// Step 2 seconds
		flight.update(2f);
		
		float expectedX = -250f * 1/2 * 2/3600;
		float expectedY = 250f * (float)Math.sqrt(3)/2 * 2/3600;
		
		// Distance is 250 * (2/3600) nm to the west
		Assert.assertEquals(expectedX, aircraft.getPosition().getX(), WITHIN_PRECISION);
		Assert.assertEquals(expectedY, aircraft.getPosition().getY(), WITHIN_PRECISION);
		
	}
	
	@Test
	public void testThatNewFlightIsAssignedUniqueFlightNumber()
	{
		Flight first = new Flight(new Aircraft());
		Flight second = new Flight(new Aircraft());
		
		Assert.assertTrue(first.getCallsign().matches("[A-Z]{3}[0-9]{3,4}"));
		Assert.assertTrue(second.getCallsign().matches("[A-Z]{3}[0-9]{3,4}"));
		Assert.assertNotSame(first.getCallsign(), second.getCallsign());
	}
	
	@Test
	public void testThatToStringReturnsCallsign()
	{
		Flight flight = new Flight(new Aircraft());
		Assert.assertEquals(flight.getCallsign(), flight.toString());
	}
	
}
